package com.basic.controller;

import com.basic.entity.Product;
import com.google.common.collect.Lists;
import org.thymeleaf.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MaController {

  public static void main(String[] args) {
      Product product1 = new Product(1L,1,new BigDecimal("15.5"),"面包","零食");
      Product product2 = new Product(2L,2,new BigDecimal("20"),"饼干","零食");
      Product product3 = new Product(3L,3,new BigDecimal("30"),"青岛啤酒","啤酒");
      Product product4 = new Product(4L,4,new BigDecimal("15.5"),"百威啤酒","啤酒");
      List<Product> list = Lists.newArrayList(product1,product2,product3,product4);
        list.stream().map(T -> setData(T)).collect(Collectors.toList());
     //转为map
      Map<Long , String > idMap = list.stream().collect(Collectors.toMap(Product::getId,Product::getName));
      System.out.println("转为map==idMap = " + idMap);
      //分组
      Map<String,List<Product>> groupListMap = list.stream().collect(Collectors.groupingBy(Product::getCategory));
      System.out.println("分组==groupListMap = " + groupListMap);
      //拼接
      String nameJoin = list.stream().map(Product::getName).collect(Collectors.joining(","));
      System.out.println("拼接==nameJoin = " + nameJoin);
      //统计集合的总数
//      Long count = list.stream().collect(Collections.counting());
      //将集合通过某个参数进行拼接（忽略空值）

  }


  private static Product setData(Product t){
      if(StringUtils.equals(t.getName(),"青岛啤酒")){
          t.setName("哈尔滨啤酒");
      }
      return t;
  }

}
